package JavaUtils.web;

import JavaUtils.CSUtils.CBytes;
import JavaUtils.CSUtils.Log.CLog;

import java.util.List;
import java.util.Map;

public class FormDataUtil {
    static final String tag = "FormDataUtil";
    static final String _formDataHead = "------WebKitFormBoundary";

    static byte[] formDataTagBytes;

    static void _init() {
        if (formDataTagBytes != null) {
            return;
        }
        formDataTagBytes = _formDataHead.getBytes();
    }

    public static void parse(List<byte[]> bytesList, Map<String, String> dataMap) {

    }

    public static void parse(byte[] bytes, Map<String, String> dataMap) {

    }

    final static boolean isTest = false;

    public static void getData(List<byte[]> dataBytesList, long fileStartPos, long fileEndPos) {
        int size = dataBytesList.size();
        String[] dataArr0 = new String[size];
        String[] dataArr1 = new String[size];
        if (isTest) {
            for (int i = 0; i < dataArr0.length; i++) {
                dataArr0[i] = new String(dataBytesList.get(i));
            }
        }

        if (size == 0) {
            CLog.ex(tag, "数据长度==0");
            return;
        } else if (size == 1) {
            byte[] bytes = dataBytesList.get(0);
            int startPos = FormDataUtil.getValueStartPos(bytes);
            int endPos = FormDataUtil.getValueEndPos(bytes);
            if (startPos < 0 || endPos < 0) {
                CLog.exf(tag, "错误.....startPos=%d.....endPos=$d", startPos, endPos);
            }
            bytes = CBytes.subBytes(bytes, startPos, endPos - startPos);
            dataBytesList.set(0, bytes);
        } else {
            final int minLen = 400;
            int byteStartIndex = 0;
            byte[] byteStart = dataBytesList.get(byteStartIndex);
            while (byteStart.length == 0) {
                byteStartIndex++;
                byteStart = dataBytesList.get(byteStartIndex);
                if (byteStart.length < minLen) {
                    dataBytesList.set(byteStartIndex, new byte[0]);
                    byteStartIndex++;
                    byte[] byteStart2 = dataBytesList.get(byteStartIndex);
                    byteStart = CBytes.merge(byteStart, byteStart2);
                    dataBytesList.set(byteStartIndex, byteStart);
                }
            }
            int startPos = FormDataUtil.getValueStartPos(byteStart);
            dataBytesList.set(byteStartIndex, CBytes.subBytes(byteStart, startPos));

            int byteEndIndex = size - 1;
            byte[] byteEnd = dataBytesList.get(byteEndIndex);
            if (byteEnd.length < minLen) {
                dataBytesList.set(byteEndIndex, new byte[0]);
                byteEndIndex--;
                byte[] byteEnd2 = dataBytesList.get(byteEndIndex);
                byteEnd = CBytes.merge(byteEnd2, byteEnd);
                dataBytesList.set(byteEndIndex, byteEnd);
            }
            int endPos = FormDataUtil.getValueEndPos(byteEnd);
            dataBytesList.set(byteEndIndex, CBytes.subBytes(byteEnd, 0, endPos));
        }
        long len = 0;
        for (byte[] bytes : dataBytesList) {
            len += bytes.length;
        }

        long calcLen = fileEndPos - fileStartPos;
        if (len != calcLen) {
            if (isTest) {
                for (int i = 0; i < dataArr0.length; i++) {
                    dataArr1[i] = new String(dataBytesList.get(i));
                }
            }
            CLog.exf(tag, "长度不相等...len=%s!=%s......fileStartPos=%s....fileEndPos=%s", len, calcLen, fileStartPos, fileEndPos);
        }
    }

    static int getValueStartPos(byte[] bytes) {
        _init();
        int index = CBytes.indexOf(bytes, formDataTagBytes);
        if (index >= 0) {
            for (int i = index + 4; i < bytes.length; i++) {
                if (bytes[i - 3] == 13 && bytes[i - 2] == 10 && bytes[i - 1] == 13 && bytes[i] == 10) {
                    index = i;
                    break;
                }
            }
        }
        return index + 1;
    }

    static int getValueEndPos(byte[] bytes) {
        _init();
        int index = CBytes.lastIndexOf(bytes, formDataTagBytes);
        return index - 2;
    }
}
